package zju.lzy.mapper;


import io.swagger.models.auth.In;
import org.apache.ibatis.annotations.*;
import zju.lzy.dto.Device;

import java.util.List;

/**
 * @author lzy
 */
@Mapper
public interface DeviceMapper extends GeneralMapper {

    @Insert("insert into device value (null,#{ownerId},#{clientPrefix},#{topic},#{qos},#{initLat},#{initLon},#{isActive},'');")
    boolean createDevice(@Param("ownerId") Integer ownerId,
                         @Param("clientPrefix") String clientPrefix,
                         @Param("topic") String topic,
                         @Param("qos") Integer qos,
                         @Param("initLat") double initLat,
                         @Param("initLon") double initLon,
                         @Param("isActive") boolean isActive);

    @Update("update device set clientPrefix=#{clientPrefix},initLat=#{initLat},initLon=#{initLon} where deviceId=#{deviceId};")
    boolean alterDevice(Device device);

    @Select("select * from device where deviceId=#{id};")
    Device getDeviceById(@Param("id") Integer deviceId);


    @Select("select * from device where ownerId=#{ownerId}")
    List<Device> getDevicesByOwnerId(@Param("ownerId") Integer ownerId);

    @Select("select * from device where isActive=true;")
    List<Device> getAllActiveDevices();

    @Select("select * from device;")
    List<Device> getAllDevices();

    @Update("update device set isActive=true where deviceId=#{id};")
    boolean startDevice(@Param("id") Integer deviceId);

    @Update("update device set isActive=false where deviceId=#{id};")
    boolean stopDevice(@Param("id") Integer deviceId);

    @Delete("delete from device where deviceId=#{id};")
    boolean deleteDevice(@Param("id") Integer deviceId);

    @Insert("insert into test (id,time) values (#{id},#{ts})")
    boolean insertTest(@Param("id") int id, @Param("ts") int ts);

    @Select("select COUNT(deviceId) from device;")
    Integer getDeviceNum();

    @Select("select COUNT(deviceId) from device where isActive=true;")
    Integer getActiveDeviceNum();

    @Select("select COUNT(distinct topic) from device;")
    Integer getTopicNum();

    @Select("select count(distinct topic) from device where isActive=true;")
    Integer getActiveTopicNum();

    @Update("update device set feature=#{feature} whrere deviceId=#{deviceId};")
    boolean updateDeviceFeatureById(@Param("id") Integer deviceId, @Param("feature") String feature);
}
